package jp.seraph.same.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jp.seraph.same.controller.MotionController;
import jp.seraph.same.model.MotionCollectionModel;
import jp.seraph.same.model.MotionModel;
import jp.seraph.same.model.PoseCollectionModel;

/**
 * モーション編集画面用最上位パネル。 このパネル上には ・すべてのモーションリスト ・すべてのポーズリスト
 * ・現在選択されている（編集中の）モーションが含むポーズ ・その他ボタン類 が表示されている。
 * 
 */
public class MotionPanel extends JPanel {
    public MotionPanel(MotionCollectionModel aMotionCollection, PoseCollectionModel aPoseCollection, MotionController aController) {
        mMotionCollection = aMotionCollection;
        mPoseCollection = aPoseCollection;
        mController = aController;
        mMotionListModel = new DefaultListModel();
        mMotionPoseListModel = new DefaultListModel();
        mAllPoseListModel = new DefaultListModel();
        mMotionList = new JList(mMotionListModel);
        mMotionPoseList = new JList(mMotionPoseListModel);
        mAllPoseList = new JList(mAllPoseListModel);
        mPoseAddButton = new JButton("Add");
        mPoseRemoveButton = new JButton("Remove");
        mRunMotionButton = new JButton("Run");
        mSaveMotionButton = new JButton("Save");
        mDeleteMotionButton = new JButton("Delete");
        mMotionNameField = new JTextField();

        initializeComponents();
        initializeEvent();
        refresh();
    }

    private void initializeEvent() {
        mPoseAddButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String tSelectedPoseName = (String) mAllPoseList.getSelectedValue();
                if (tSelectedPoseName == null)
                    return;

                mController.addPose(tSelectedPoseName);
                refresh();
            }
        });

        mPoseRemoveButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String tSelectedPoseName = (String) mMotionPoseList.getSelectedValue();
                if (tSelectedPoseName == null)
                    return;

                mController.removePose(tSelectedPoseName);
                refresh();
            }
        });

        mRunMotionButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                mController.runMotion();
            }
        });

        mSaveMotionButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String tMotionName = mMotionNameField.getText();
                mController.setMotion(tMotionName);
                refresh();
            }
        });

        mDeleteMotionButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String tSelectedMotionName = (String) mMotionList.getSelectedValue();
                if (tSelectedMotionName == null)
                    return;

                mController.removeMotion(tSelectedMotionName);
                refresh();
            }
        });

        mMotionList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int tViewSelected = mMotionList.getSelectedIndex();
                int tModelSelected = mMotionCollection.getSelectedIndex();
                if (tViewSelected == tModelSelected)
                    return;

                String tSelectedMotionName = (String) mMotionList.getSelectedValue();
                if (tSelectedMotionName == null)
                    return;

                mController.selectMotion(tSelectedMotionName);
                mMotionNameField.setText(tSelectedMotionName);
                refresh();
            }
        });
    }

    private void initializeComponents() {
        this.setLayout(new GridLayout(1, 5));
        this.add(createAllPoseList());
        this.add(createEditMotionPanel());
        this.add(createMotionPoseList());
        this.add(createControlMotionPanel());
        this.add(createMotionList());
    }

    private Component createAllPoseList() {
        JScrollPane tPane = new JScrollPane();
        tPane.setViewportView(mAllPoseList);
        return tPane;
    }

    private Component createMotionPoseList() {
        JScrollPane tPane = new JScrollPane();
        tPane.setViewportView(mMotionPoseList);
        return tPane;
    }

    private Component createMotionList() {
        JScrollPane tPane = new JScrollPane();
        tPane.setViewportView(mMotionList);
        return tPane;
    }

    private Component createEditMotionPanel() {
        /*
         * JPanel tResult = new JPanel(); tResult.setLayout(new FlowLayout());
         */

        Box tResult = new Box(BoxLayout.Y_AXIS);

        tResult.add(Box.createVerticalStrut(140));
        tResult.add(mPoseAddButton);
        tResult.add(mPoseRemoveButton);
        tResult.add(Box.createGlue());

        return tResult;
    }

    private Component createControlMotionPanel() {
        Box tResult = new Box(BoxLayout.Y_AXIS);

        mMotionNameField.setPreferredSize(new Dimension(100, 26));

        JPanel tInnerPanel = new JPanel(new FlowLayout());

        tResult.add(Box.createVerticalStrut(100));
        tInnerPanel.add(mMotionNameField);
        tInnerPanel.add(mSaveMotionButton);
        tInnerPanel.add(mDeleteMotionButton);
        tInnerPanel.add(mRunMotionButton);
        tResult.add(tInnerPanel);
        tResult.add(Box.createVerticalGlue());

        return tResult;
    }

    private JList mMotionList;
    private DefaultListModel mMotionListModel;
    private JList mMotionPoseList;
    private DefaultListModel mMotionPoseListModel;
    private JList mAllPoseList;
    private DefaultListModel mAllPoseListModel;
    private MotionCollectionModel mMotionCollection;
    private PoseCollectionModel mPoseCollection;
    private MotionController mController;
    private JTextField mMotionNameField;
    private JButton mSaveMotionButton;
    private JButton mDeleteMotionButton;
    private JButton mPoseAddButton;
    private JButton mPoseRemoveButton;
    private JButton mRunMotionButton;
    private static final long serialVersionUID = 10L;

    public void refresh() {
        int tSelectedMotionIndex = mMotionCollection.getSelectedIndex();
        mMotionListModel.clear();
        mMotionPoseListModel.clear();
        mAllPoseListModel.clear();

        int tLength;
        tLength = mMotionCollection.getMotionCount();
        for (int i = 0; i < tLength; i++) {
            mMotionListModel.addElement(mMotionCollection.getMotionName(i));
        }
        mMotionList.setSelectedIndex(tSelectedMotionIndex);

        tLength = mPoseCollection.getPoseCount();
        for (int i = 0; i < tLength; i++) {
            mAllPoseListModel.addElement(mPoseCollection.getPoseName(i));
        }

        MotionModel tSelectedModel = mMotionCollection.getSelectedMotion();
        tLength = tSelectedModel.getPoseCount();
        for (int i = 0; i < tLength; i++) {
            mMotionPoseListModel.addElement(tSelectedModel.getPoseName(i));
        }
    }
}